/*
 * Copyright 2009, eGDP LTD, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.egdp.jclpvm.core.ioc;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.nativelibs4java.opencl.CLDevice;

/**
 * Implements abstract of proxied "CLDevice component".
 * Final bean will be dynamically injected into application context.
 *
 * @author Alex Sorkin alexander.sorkin@gmail.com
 * @since 0.0.1
 * @see ProxyCLPlatform
 * @see ProxyCLContext
 */
public abstract class ProxyCLDevice implements ICLContainerDevice {

	protected static Log logger = LogFactory.getLog(ProxyCLDevice.class);
	
	@Autowired
	protected ICLContainerPlatform jclpvm_platform;
	private final PropertyChangeSupport changes  = new PropertyChangeSupport( this );
	
	private CLDevice legal_cldevice;

	/**
	 * @return the jclpvm_platform
	 */
	protected ICLContainerPlatform getJclpvm_platform() {
		return jclpvm_platform;
	}

	/**
	 * @param jclpvmPlatform the jclpvm_platform to set
	 */
	protected void setJclpvm_platform(ICLContainerPlatform jclpvmPlatform) {
		this.jclpvm_platform = jclpvmPlatform;
	}

	/**
	 * @return the legal_cldevice
	 */
	public CLDevice getLegal_cldevice() {
		return legal_cldevice;
	}
	
	public void releaseLegal_cldevice(){
		if (legal_cldevice != null)
			legal_cldevice.release();
		logger.debug("Current legal device has released");
	}

	/**
	 * To get callback support with depended beans
	 */
	public void addPropertyChangeListener(final PropertyChangeListener l) {  
        this.changes.addPropertyChangeListener( l );  
    }   
	public void removePropertyChangeListener(final PropertyChangeListener l) {  
        this.changes.removePropertyChangeListener( l );  
    }
}
